﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using BigFileUploadDemo.Models;
using Minio;
using Microsoft.AspNetCore.Http;
using Minio.DataModel.Args;
using Minio.Exceptions;
using CommunityToolkit.HighPerformance.Helpers;
using BigFileUploadDemo.Lib;

namespace BigFileUploadDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IMinioClient _minioClient;
        private readonly IMinioClientFactory _minioClientFactory;
        private readonly IResponseHelper _resp;
        public HomeController(ILogger<HomeController> logger,IMinioClient minioClient, IResponseHelper responseHelper, IMinioClientFactory minioClientFactory)
        {
            _logger = logger;
            _minioClient = minioClient;
            _resp = responseHelper;
            _minioClientFactory = minioClientFactory;
        }

        [HttpGet]
        [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
        public async Task<IActionResult> GetUrl()
        {
            try
            {
                var buckets = await _minioClient.ListBucketsAsync();
                foreach (var bucket in buckets.Buckets)
                {
                    Console.WriteLine(bucket.Name + " " + bucket.CreationDateDateTime);
                }

                var args = new MakeBucketArgs().WithBucket(Guid.NewGuid().ToString()).WithLocation("baoding");
                await _minioClient.MakeBucketAsync(args);
            }
            catch (MinioException e)
            {
                Console.WriteLine("Error occurred: " + e);
            }

            return Ok(_resp.success(6));
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
